package com.xyz.study.player;

import org.springframework.util.StringUtils;

/**
 * 测试类
 * @author XYZ
 * @version 1.0
 * @since 2021/10/18
 */
public class TestMain {

    public static void main(String[] args) {
        PlayerManager manager = new PlayerManagerImpl();
        new Thread(new PlayManagerTestTask(new PlayerImpl("xiaoming", true), "", manager)).start();
        new Thread(new PlayManagerTestTask(new PlayerImpl("xiaohu", false), "", manager)).start();
        new Thread(new PlayManagerTestTask(new PlayerImpl("xiaowei", true), "", manager)).start();
        new Thread(new PlayManagerTestTask(new PlayerImpl("guozige", false), "", manager)).start();
        new Thread(new PlayManagerTestTask(new PlayerImpl("gala8", true), "", manager)).start();
    }

    /**
     * 测试加入玩家并广播消息
     */
    private static class PlayManagerTestTask implements Runnable{
        private Player player;
        private String message;
        private PlayerManager manager;

        PlayManagerTestTask(Player player, String message, PlayerManager manager) {
            this.message = message;
            this.manager = manager;
            this.player = player;
        }

        @Override
        public void run() {
            manager.addPlayer(player);
            if (StringUtils.isEmpty(message)){
                manager.broadcast("Player " + player.getUsername() + "已加入！");
            }else {
                manager.broadcast(message);
            }
        }
    }
}
